package com.barunsw.bwf.ems.neoarena.ne.pct.mgcp;

import java.util.Vector;

public class MgcpControl
{
	final int	PACKET_SIZE = 2048;
	final int   ERROR_LENGTH = 128;

	private int seqSize;
	private java.io.DataInputStream stream = null;

	public MgcpControl(Vector vData)
	{
		int idx = 0;
		try
		{
			byte[] newData = null;
			this.seqSize = vData.size();

			if ( seqSize > 1 ) {
				newData = new byte[seqSize * PACKET_SIZE];
			}
			else {
				char[] data = (char[])vData.get(0);
				newData = new byte[data.length];
			}

			for ( int i = 0; i < seqSize; i++ ) {
				char[] data = (char[])vData.get(i);
				for ( int j = 0; j < PACKET_SIZE; j++ )
					newData[idx++] = (byte)data[j];
			}

			stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public M3018_MODE[] getDisDspUse()
	{

		M3018_MODE mode = new M3018_MODE();
		M3018_MODE[] mlist = new M3018_MODE[mode.MAX_VOC_BD];

		try
		{
			for(int i=0; i<mode.MAX_VOC_BD; i++){

				M3018_MODE _mode = new M3018_MODE();

				_mode.vocAvlCnt = stream.readShort();
				_mode.vocAlocCnt = stream.readShort();
System.out.println("mode[" + i + "]:" + _mode.vocAlocCnt);				
				_mode.loadFlag = stream.readShort();
				stream.readShort(); //dummy
				for(int j=0; j<mode.MAX_VOCUSE_LEN; j++){
					_mode.vocUse[j] = stream.readInt();
				}
				mlist[i] = _mode;
			}
			return mlist;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3502_MODE getDisRgwMgc()
	{
		M3502_MODE mode = new M3502_MODE();

		try
		{
			byte[] ipaddr = new byte[mode.MAX_IPADDR_LEN];
			byte[] domain = new byte[mode.MAX_IPADDR_LEN];

			stream.read(ipaddr, 0, ipaddr.length);
			mode.ip_addr = (new String(ipaddr)).trim();

			stream.read(domain, 0, domain.length);
			mode.domain = (new String(domain)).trim();

			mode.tx_port = stream.readInt();
			mode.rx_port = stream.readInt();
			mode.status = stream.readInt();

			stream.read(ipaddr, 0, ipaddr.length);
			mode.ip_addr2 = (new String(ipaddr)).trim();

			stream.read(domain, 0, domain.length);
			mode.domain2 = (new String(domain)).trim();


			mode.tx_port2 = stream.readInt();
			mode.rx_port2 = stream.readInt();

			byte[] hostName = new byte[mode.MAX_HOSTNAME_LEN];

			stream.read(hostName, 0, hostName.length);
			String hostRawData = (new String(hostName)).trim();
System.out.println("M3502_MODE host_name rawData : <" + hostRawData + ">");
			String token[] = hostRawData.split("\0");
			mode.host_name = token[0];
System.out.println("M3502_MODE host_name : <" + mode.host_name + ">");

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3507_MODE[] getDisRgwCodec()
	{
		M3507_MODE mode = new M3507_MODE();
		M3507_MODE[] modeList = new M3507_MODE[mode.MAX_CODEC_INFO];

		try {
			for (int i=0; i<mode.MAX_CODEC_INFO; i++) {
				M3507_MODE m = new M3507_MODE();
				m.equip_flag = stream.readInt();
				m.codec_type = stream.readInt();
				m.g723_flag = stream.readInt();
				m.ptime = stream.readInt();
				m.tdm_gain = stream.readInt();
				m.pkt_gain = stream.readInt();
				m.echo_flag = stream.readInt();
				m.vad_cng = stream.readInt();
				m.jitter = stream.readInt();
				m.jitter_msec = stream.readInt();
				m.plc_flag = stream.readInt();

				modeList[i] = m;
			}
			
			modeList[0].echo_flag = stream.readInt();
			modeList[0].echo_nlp = stream.readInt();
			modeList[0].echo_cn = stream.readInt();
			modeList[0].echo_tail_len = stream.readInt();

			return modeList;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3520_MODE getDisRtpInf()
	{
		M3520_MODE mode = new M3520_MODE();
		byte[] ip_addr = new byte[mode.MAX_IPADDR_LEN];
		byte[] codec = new byte[mode.CODEC_LEN];
		byte[] buff = new byte[8];

		try
		{
			mode.itf_id = stream.readInt();
System.out.println("mode.itf_id : " + mode.itf_id);			
			mode.port_id = stream.readInt();
System.out.println("mode.port_id : " + mode.port_id);			
			
			stream.read(ip_addr, 0, ip_addr.length);
			mode.src_ip = (new String(ip_addr)).trim();
System.out.println("mode.src_ip : " + mode.src_ip);

			stream.read(ip_addr, 0, ip_addr.length);
			mode.dest_ip = (new String(ip_addr)).trim();
System.out.println("mode.dest_ip : " + mode.dest_ip);

			stream.read(codec, 0, codec.length);
			mode.codec = (new String(codec)).trim();
System.out.println("mode.codec : " + mode.codec);

			stream.read(buff, 0, buff.length);
			mode.ptime = (new String(buff)).trim();
System.out.println("mode.ptime : " + mode.ptime);

			stream.read(buff, 0, buff.length);
			mode.vadcng = (new String(buff)).trim();
System.out.println("mode.vadcng : " + mode.vadcng);

			stream.read(buff, 0, buff.length);
			mode.echo = (new String(buff)).trim();
System.out.println("mode.echo : " + mode.echo);

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3548_MODE getDisDscp()
	{
		M3548_MODE mode = new M3548_MODE();

		try
		{
			mode.mgcp_dscp = stream.readInt();
			mode.rtp_dscp = stream.readInt();

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3545_MODE getDisDtmfInband()
	{
		M3545_MODE mode = new M3545_MODE();

		try
		{
			mode.dtmf_flag = stream.readInt();

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3113_MODE[] getDisDataMedia() {
		M3113_MODE[] modeList = new M3113_MODE[M3113_MODE.MAX_MEDIA_INFO];

		try {
			for ( int i = 0; i < M3113_MODE.MAX_MEDIA_INFO; i++ ) {
				M3113_MODE m = new M3113_MODE();
				m.vad_cng_flag = stream.readInt();
				m.echo_flag = stream.readInt();
				m.echo_nlp = stream.readInt();
				m.echo_cn_mode = stream.readInt();
				m.echo_tail_len = stream.readInt();
				m.jitter_type = stream.readInt();
				m.jitter_buf_init_size = stream.readInt();
				m.jitter_buf_min_size = stream.readInt();
				m.jitter_buf_max_size = stream.readInt();

				modeList[i] = m;
			}

			return modeList;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}

	public M3540_MODE[] getDisCnfgDmap() {
		try{
			Vector<M3540_MODE> dmapInfo = new Vector();
			byte mv[] = new byte[M3540_MODE.MAX_COL];

			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);

				int total_map_count = stream.readInt();

				int map_count = stream.readInt();
				for ( int j = 0; j < map_count; j++ ) {
					stream.read(mv, 0, mv.length);

					M3540_MODE oneMode = new M3540_MODE();
					
					String mapValue = new String(mv).trim();
System.out.println("mapValue1[" + j + "]:" + mapValue);					
					int index = mapValue.indexOf("\0");
					if ( index >= 0 ) {
						mapValue = mapValue.substring(0, index);
System.out.println("mapValue2[" + j + "]:" + mapValue);						
					}
					
					oneMode.map_value = mapValue;
					
					dmapInfo.addElement(oneMode);
				}
			}
			
			int dmapInfoSize = dmapInfo.size();
			M3540_MODE[] modeSeq = new M3540_MODE[dmapInfoSize];
			for ( int i = 0; i < dmapInfoSize; i++ ) {
				modeSeq[i] = dmapInfo.elementAt(i);
			}
			
			return modeSeq;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}
	
	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
